﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Mapping.Attributes;
using SuperMarketManagement.Entity;

namespace SuperMarketManagement.DAO.NHibernate
{
    class SessionProvider
    {
        internal static ISessionFactory SessionFactory { get; private set; }
        static SessionProvider()
        {
            var configuration = new global::NHibernate.Cfg.Configuration();
            Type example = typeof (Employee);
            HbmSerializer.Default.HbmNamespace = example.Namespace;
            HbmSerializer.Default.HbmAssembly = example.Assembly.FullName;
            HbmSerializer.Default.HbmAutoImport = true;
            using (var stream = new MemoryStream())
            {
                HbmSerializer.Default.Serialize(stream, example.Assembly);
                stream.Position = 0;
                configuration.AddInputStream(stream);
                configuration.Configure();
                SessionFactory = configuration.BuildSessionFactory();
            }

        }
        internal static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }

        internal static ISession GetCurrentSession()
        {
            return SessionFactory.GetCurrentSession();
        }
    }
}